package com.zhu.storesmanager.views;

import java.util.List;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SvStorePromoterInfoActivity;
import com.zhu.storesmanager.models.ProjectDataItem;
import com.zhu.storesmanager.models.PromoterInfoItem;
import com.zhu.storesmanager.provider.StoresManagerSettings;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.DatabaseUtil;
import com.zhu.storesmanager.utils.ViewsUtil;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 促销员信息界面
 */
public class StoresPromoterView extends ScrollView implements View.OnClickListener{

    private PromoterInfoItem mPromoterInfoItem = null; //促销员数据
    private SvStorePromoterInfoActivity mActivity = null; //Activity
    private RadioGroup mInpostRadioGroup = null; //在岗情况
    private RadioGroup mImageRadioGroup = null; //促销员形象
    private RadioGroup mStatusRadioGroup = null; //促销员状态
    private MoreRadioGroup mCheckRadioGroup = null; //促销员考勤
    private RadioGroup mTmpPRadioGroup = null; //临促情况
    private TextView mBadReasonView = null; //不好原因
    private TextView mNotInpostView = null; //不在岗原因
    private TextView mProblemView = null; //问题
    private EditText mPNameView = null; //促销员名字
    private EditText mPTelView = null; //促销员电话
    private PhotoView mPhotoView = null; //拍照
    private PopEditText mPopEditText = null;

    public StoresPromoterView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public StoresPromoterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public StoresPromoterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /**
     * 界面加载回调函数
     * (non-Javadoc)
     * @see android.view.View#onFinishInflate()
     */
    public void onFinishInflate(){
    	//初始化界面
    	mInpostRadioGroup = setRadioGroupData(R.id.promoter_inpost, R.string.promoter_inpost, R.string.dialog_yes,
    			R.string.dialog_no, R.string.promoter_select_xiu, -1, -1);
    	mImageRadioGroup = setRadioGroupData(R.id.promoter_image, R.string.promoter_iamge, R.string.promoter_select_hao,
    			R.string.promoter_select_jiaohao, R.string.promoter_select_cha, -1, -1);
    	mStatusRadioGroup = setRadioGroupData(R.id.promoter_status, R.string.promoter_status, R.string.promoter_select_hao,
    			R.string.promoter_select_buhao, -1, -1, -1);
    	mCheckRadioGroup = (MoreRadioGroup)findViewById(R.id.promoter_check);
    	mCheckRadioGroup.setTitle(R.string.promoter_check);
    	mTmpPRadioGroup = setRadioGroupData(R.id.promoter_tmppromoter, R.string.promoter_tmppromoter, R.string.dialog_yes,
    			R.string.dialog_no, R.string.promoter_select_wu, -1, -1);
    	mBadReasonView = setTextViewData(R.id.promoter_badreason, R.string.promoter_badreason);
    	mBadReasonView.setOnClickListener(this);
    	mNotInpostView = setTextViewData(R.id.promoter_tmppnotpost, R.string.promoter_tmppnotpost);
    	mNotInpostView.setOnClickListener(this);
    	mProblemView = setTextViewData(R.id.promoter_problem, R.string.promoter_problem);
    	mProblemView.setOnClickListener(this);
    	mPNameView = setEditTextData(R.id.promoter_pname, R.string.promoter_tmppname);
    	mPTelView = setEditTextData(R.id.promoter_ptel, R.string.promoter_tmpptel);
    	mPhotoView = (PhotoView)findViewById(R.id.promoter_photo);
    }
    

    /**
     * 设置数据
     * @param activity
     * @param item 数据
     * @param popEditText 弹窗输入框
     */
    public void setData(SvStorePromoterInfoActivity activity, PopEditText popEditText, PromoterInfoItem item){
        mActivity = activity;
        mPromoterInfoItem = item;
        mPhotoView.initData(activity, R.string.promoter_photo);
        mPopEditText = popEditText; 
        setViewData();
    }

    /**
     * 设置界面数据
     */
    private void setViewData(){
    	int iInpost = mPromoterInfoItem.mInpost;
		int iImage = mPromoterInfoItem.mImage;
		int iStatus= mPromoterInfoItem.mStatus;
		int iTmpInfo = mPromoterInfoItem.mTemporary;
		int iCheck = mPromoterInfoItem.mCheck;
		mCheckRadioGroup.setSelectedIndex(iCheck);
		setRadioGroupSelectedData(iInpost, mInpostRadioGroup);
		setRadioGroupSelectedData(iImage, mImageRadioGroup);
		setRadioGroupSelectedData(iStatus, mStatusRadioGroup);
		setRadioGroupSelectedData(iTmpInfo, mTmpPRadioGroup);
		mBadReasonView.setText(mPromoterInfoItem.mBadReason);
		mNotInpostView.setText(mPromoterInfoItem.mNotPostReason);
		mProblemView.setText(mPromoterInfoItem.mProblem);
		mPNameView.setText(mPromoterInfoItem.mTempName);
		mPTelView.setText(mPromoterInfoItem.mTempTel);
		mPhotoView.setImageData(mPromoterInfoItem.mPhoto);

		updateAdminView();//更新管理员界面
    }

    /**
     * 设置RadioGroup选中数据
     * index 选中项
     * radioGroup
     */
    private void setRadioGroupSelectedData(int index, RadioGroup radioGroup){
    	if(index != -1){
    		radioGroup.check(radioGroup.getChildAt(index).getId());   
    	}
    }

    /**
     * 获取促销员ID
     */
    public String getPromoterID(){
        return mPromoterInfoItem.mPromoterId;
    }

    /**
	 * 设置RadioGroup界面文本
	 * @return radioGroup
	 * @param parentId 父ID
	 * @param textId 文本id
	 * @param text1Id 选择1文本
	 * @param text2Id 选择2文本
	 * @param text3Id 选择3文本
	 * @param text4Id 选择4文本
	 * @param text5Id 选择5文本
	 */
	private RadioGroup setRadioGroupData(int parentId, int textId, int text1Id, int text2Id, int text3Id, int text4Id, int text5Id){
		View view = findViewById(parentId);
		RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.radiogroup);
		((TextView)view.findViewById(R.id.radiogroup_label)).setText(textId);
		if(text1Id != -1){
			((RadioButton)view.findViewById(R.id.radiobutton1)).setText(text1Id);	
		}
		if(text2Id != -1){
			((RadioButton)view.findViewById(R.id.radiobutton2)).setText(text2Id);
		}
		if(text3Id != -1){
			((RadioButton)view.findViewById(R.id.radiobutton3)).setText(text3Id);
		}
		if(text4Id != -1){
			((RadioButton)view.findViewById(R.id.radiobutton4)).setText(text4Id);
		}
		if(text5Id != -1){
			((RadioButton)view.findViewById(R.id.radiobutton5)).setText(text5Id);
		}
		return radioGroup;
	}

	 /**
	 * 设置RadioGroup界面文本
	 * @param parentId 父ID
	 * @param text文本
	 */
	private void setRadioGroupText(int parentId, String text){
		View view = findViewById(parentId);
		((TextView)view.findViewById(R.id.radiogroup_label)).setText(text);
	}

    /**
	 * 设置文本界面数据
	 * @return TextView
	 * @param parentId 父ID
	 * @param textId 文本id
	 */
	private TextView setTextViewData(int parentId, int textId){
		View view = findViewById(parentId);
		((TextView)view.findViewById(R.id.sv_edittext_label)).setText(textId);
		return (TextView)view.findViewById(R.id.sv_edittext);
	}

	/**
	 * 设置文本界面数据
	 * @param parentId 父ID
	 * @param text 文本
	 */
	private void setTextViewData(int parentId, String text){
		View view = findViewById(parentId);
		((TextView)view.findViewById(R.id.sv_edittext_label)).setText(text);
	}

	/**
	 * 设置输入框界面数据
	 * @return EditText
	 * @param parentId 父ID
	 * @param textId 文本id
	 */
	private EditText setEditTextData(int parentId, int textId){
		View view = findViewById(parentId);
		((TextView)view.findViewById(R.id.sv_edittext_label)).setText(textId);
		return (EditText)view.findViewById(R.id.sv_edittext);
	}

	/**
	 * 设置输入框界面数据
	 * @param parentId 父ID
	 * @param text 文本
	 */
	private void setEditTextText(int parentId, String text){
		View view = findViewById(parentId);
		((TextView)view.findViewById(R.id.sv_edittext_label)).setText(text);
	}

	/**
	 * 拍照数据返回
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		mPhotoView.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 保存数据
	 */
	public void saveData(){
		ContentValues values = new ContentValues();
		//所有RadioGroup选中值
		int iInpost = getRadioGroupSelectedIndex(mInpostRadioGroup);
		int iImage = getRadioGroupSelectedIndex(mImageRadioGroup);
		int iStatus= getRadioGroupSelectedIndex(mStatusRadioGroup);
		int iTmpInfo = getRadioGroupSelectedIndex(mTmpPRadioGroup);
		int iCheck = mCheckRadioGroup.getSelectedIndex();

		values.put(StoresManagerSettings.SAVED, 0);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_INPOST, iInpost);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_IMAGE, iImage);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_STATUS, iStatus);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_TMPINFO, iTmpInfo);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_CHECK, iCheck);
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_BADREASON, mBadReasonView.getText().toString());
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_TMPNAME, mPNameView.getText().toString());
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_TMPTEL, mPTelView.getText().toString());
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_PROBLEM, mProblemView.getText().toString());
		values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_NOTPOSTREASON, mNotInpostView.getText().toString());
		if(mPhotoView.isNeedSavedData()){
			values.put(StoresManagerSettings.SupervisorStorePromoterColumns.PROMOTER_PHOTO, mPhotoView.getBitmapData());
		}
		if(mPromoterInfoItem.mDBid == -1){ //如果不存在数据 则插入新数据
			Uri uri = getContext().getContentResolver().insert(StoresManagerSettings.SupervisorStorePromoterColumns.CONTENT_URI_NO_NOTIFICATION, values);
			mPromoterInfoItem.mDBid = Long.parseLong(uri.getPathSegments().get(1));
		}else{ //存在则更新历史数据
			getContext().getContentResolver().update(StoresManagerSettings.SupervisorStorePromoterColumns.getContentUri(mPromoterInfoItem.mDBid, false), values, null, null);
		}
	}

	/**
	 * 获取选中项索引
	 * @param radioGroup
	 * @return index 选中项索引
	 */
	private int getRadioGroupSelectedIndex(RadioGroup radioGroup){
		int index = -1;
		index = radioGroup.getCheckedRadioButtonId();
		if(index != -1){
			index = radioGroup.indexOfChild(radioGroup.findViewById(index));
		}
		return index;
	}

	/**
	 * (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int textID = -1;
		if(mBadReasonView == v){
			textID = R.string.promoter_badreason;
		}else if(mNotInpostView == v){
			textID = R.string.promoter_tmppnotpost;
		}else if(mProblemView == v){
			textID = R.string.promoter_problem;
		}
		mPopEditText.showPopEditText(textID, (TextView)v);
	}

	/**
	 * 更新project数据从数据库
	 * @param cursor 数据库数据
	 */
	public void updateProjectDataFromCursor(List<ProjectDataItem> list ){
		if(list != null && list.size() > 0){
			for(ProjectDataItem item : list){
				updateViewFromProject(item);
			}
		}
	}

    /**
	 * 从project数据更新界面
	 */
	private void updateViewFromProject(ProjectDataItem item) {
		if (item.mId == 1) { //在岗情况
			if (item.isShow) {
				setRadioGroupText(R.id.promoter_inpost, item.mName);
			} else {
				findViewById(R.id.promoter_inpost).setVisibility(View.GONE);
			}
		} else if (item.mId == 2) { //促销员形象
			if (item.isShow) {
				setRadioGroupText(R.id.promoter_image, item.mName);
			} else {
				findViewById(R.id.promoter_image).setVisibility(View.GONE);
			}
		} else if (item.mId == 3) { //促销员状态
			if (item.isShow) {
				setRadioGroupText(R.id.promoter_status, item.mName);
			} else {
				findViewById(R.id.promoter_status).setVisibility(View.GONE);
			}
		} else if (item.mId == 4) { //不好原因
			if (item.isShow) {
				setTextViewData(R.id.promoter_badreason, item.mName);
			} else {
				findViewById(R.id.promoter_badreason).setVisibility(View.GONE);
			}
		}else if(item.mId == 5){ //促销员考勤
			if (item.isShow) {
				mCheckRadioGroup.setTitle(item.mName);
			} else {
				mCheckRadioGroup.setVisibility(View.GONE);
			}
		}else if(item.mId == 6){ //临促情况
			if (item.isShow) {
				setRadioGroupText(R.id.promoter_tmppromoter, item.mName);
			} else {
				findViewById(R.id.promoter_tmppromoter).setVisibility(View.GONE);
			}
		}else if(item.mId == 7){ //临促姓名
			if (item.isShow) {
				setEditTextText(R.id.promoter_pname, item.mName);
			} else {
				findViewById(R.id.promoter_pname).setVisibility(View.GONE);
			}
		}else if(item.mId == 8){ //临促电话
			if (item.isShow) {
				setEditTextText(R.id.promoter_ptel, item.mName);
			} else {
				findViewById(R.id.promoter_ptel).setVisibility(View.GONE);
			}
		}else if (item.mId == 9) { //不在岗原因
			if (item.isShow) {
				setTextViewData(R.id.promoter_tmppnotpost, item.mName);
			} else {
				findViewById(R.id.promoter_tmppnotpost).setVisibility(View.GONE);
			}
		}else if (item.mId == 10) { //促销员问题
			if (item.isShow) {
				setTextViewData(R.id.promoter_problem, item.mName);
			} else {
				findViewById(R.id.promoter_problem).setVisibility(View.GONE);
			}
		}else if (item.mId == 11){ //拍照
			if (item.isShow) {
				mPhotoView.setLableText(item.mName);
			} else {
				mPhotoView.setVisibility(View.GONE);
			}
		}
	}

	/**
	 * 更新管理员界面
	 */
	private void updateAdminView(){
		if(DataUtils.mUserData.isAdmin()){ //管理员
			mPhotoView.setVisibility(View.GONE);
			ViewsUtil.setRadioGroupdisable(mInpostRadioGroup);
			ViewsUtil.setRadioGroupdisable(mImageRadioGroup);
			ViewsUtil.setRadioGroupdisable(mStatusRadioGroup);
			ViewsUtil.setRadioGroupdisable(mTmpPRadioGroup);
			mBadReasonView.setEnabled(false);
			mNotInpostView.setEnabled(false);
			mProblemView.setEnabled(false);
			mPNameView.setEnabled(false);
			mPTelView.setEnabled(false);
			mCheckRadioGroup.setRadioGroupdisable();
    	}
	}

	/**
	 * 检测保存的数据 是否符合规则
	 * @return true 可以保存 否则反之
	 */
	public boolean checkSaveData(){
		int iStatus= getRadioGroupSelectedIndex(mStatusRadioGroup);
		String bad = mBadReasonView.getText().toString().trim();
		if(iStatus == 1){
			if(checkString(bad)){
				return false;
			}
		}
		int iTmpInfo = getRadioGroupSelectedIndex(mTmpPRadioGroup);
		if(iTmpInfo == 0 || iTmpInfo == 1){
			String name = mPNameView.getText().toString().trim();
			String tel = mPTelView.getText().toString().trim();
			String notinpost = mNotInpostView.getText().toString().trim();
			if(checkString(name) || checkString(tel) || checkString(notinpost)){
				return false;
			}
		}
		return true;
	}

	/**
	 * 检测输入数据是否完整
	 */
	public boolean checkInputData(){
		int iInpost = getRadioGroupSelectedIndex(mInpostRadioGroup);
//		int iImage = getRadioGroupSelectedIndex(mImageRadioGroup);
//		int iStatus= getRadioGroupSelectedIndex(mStatusRadioGroup);
//		int iTmpInfo = getRadioGroupSelectedIndex(mTmpPRadioGroup);
//		int iCheck = mCheckRadioGroup.getSelectedIndex();
		if(Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mActivity.getProjectList(),
				StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_PROMOTER, 1), iInpost)
				/*&& Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mActivity.getProjectList(),
						StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_PROMOTER, 2), iImage)
			    && Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mActivity.getProjectList(),
					    StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_PROMOTER, 3), iStatus)
			   && Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mActivity.getProjectList(),
				        StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_PROMOTER, 5), iCheck)
			&& Actions.checkRadioButtonData(DataUtils.isProjectDataShow(mActivity.getProjectList(),
						StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_PROMOTER, 6), iTmpInfo)*/){
			return true;
		}
		return false;
	}

	/**
	 * 检测字符串是否为空
	 * @return true 空
	 */
	private boolean checkString(String text){
		if(text == null || text.length() == 0){
			return true;
		}
		return false;
	}
}
